package com.data.modules.smzh.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.data.common.api.vo.Result;
import com.data.common.util.JwtIOTAuthUtil;
import com.data.common.util.KamliClientUtil;
import com.data.modules.smzh.entity.Facedevice;
import com.data.modules.smzh.entity.OftenContact;
import com.data.modules.smzh.mapper.FacedeviceMapper;
import com.data.modules.smzh.mapper.OftenContactMapper;
import com.data.modules.smzh.service.IOftenContactService;
import com.data.modules.smzh.vo.SmContactVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Description: often_contact
 * @Author: data-boot
 * @Date:   2023-02-28
 * @Version: V1.0
 */
@Service
public class OftenContactServiceImpl extends ServiceImpl<OftenContactMapper, OftenContact> implements IOftenContactService {

    @Autowired
    private FacedeviceMapper facedeviceMapper;

    @Override
    public Result<?> addContact(SmContactVo smContactVo, HttpServletRequest req) {

        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                reaAppId = facedeviceMapper.queryDevelAppId(appId);

                OftenContact oftenContact = new OftenContact();
                oftenContact.setAppId(Integer.valueOf(reaAppId));
                oftenContact.setCompanyAddress(smContactVo.getCompanyAddress());
                oftenContact.setCompanyName(smContactVo.getCompanyName());
                oftenContact.setContactPhone(smContactVo.getContactPhone());
                oftenContact.setCreateTime(new Date());
                oftenContact.setProjectId(smContactVo.getProjectId());

                this.save(oftenContact);
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
        return Result.ok(0,"操作成功");
    }

    @Override
    public List<OftenContact> searchPhoneInfo(String buildNo) {
        return facedeviceMapper.searchPhoneInfo(buildNo);
    }
}
